package org.vectrics.recruiter.struts;

/**
 * @author Mike Moore
 * @version 1.0
 */

import java.util.Date;

import org.apache.log4j.Category;
import org.vectrics.DataException;
import org.vectrics.SystemException;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.recruiter.user.Resume;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;

public class SaveTextResumeAction extends VectricsStrutsAction {
    private static Category log = Category.getInstance(SaveTextResumeAction.class);
    public static String INTERVIEW_PARAM = "interview";    
    public static String JOB_PARAM = "job";    
    public static String USER_NAME_PARAM = "userName";
    public static String DATE_PARAM = "date";
    public static String RESUME_TEXT_PARAM = "resumeText";
    public static String RESULT_PARAM = "result_code";


	public String doExecute(VectricsActionData actionData) {
        log.debug("execute() called");
        
        if (actionData.getForm() == null) {
        	log.error("execute() - form is null");
        } else {
    		if (actionData.wasButtonPressed("cancel") == false) {
		        ResumeForm resumeForm = (ResumeForm)actionData.getForm();
		        String personId = resumeForm.getUserId();
		        String resumeId = resumeForm.getResumeId();

		        if (personId == null) {
		        	log.error("SaveTextResume() was passed a null 'userId'");
		        	throw new SystemException("SaveTextResume() was passed a null 'userId'");
		        }
				try {
					log.debug("execute() - saving resume...");
					Resume resume = null;
					Person person = CoreServiceLocator.getPersonService().findPersonById(new Long(personId));
					CandidateInfo candidate = RecruitServiceLocator.getRecruitUserService().createCandidateInfoIfNotExist(person);
					if (candidate.getId() == null)
						RecruitServiceLocator.getRecruitUserService().updateCandidateInfo(candidate);
					if ((resumeId == null) || (resumeId.trim().length() <= 0)) {
						resume = candidate.addResume();
					}
					else {
						resume = RecruitServiceLocator.getRecruitUserService().findResume(new Long(resumeId));
						if (resume == null) {
							log.error("Could not find resume with ID = " + resumeId);
							throw new SystemException("Could not find resume with ID = " + resumeId);
						}
					}
					resume.setLastUpdate(new Date());
					resume.setName(resumeForm.getName());
					
					RecruitServiceLocator.getRecruitUserService().saveUploadedResumeToFile(candidate, resume, resumeForm.getResumeText());
					RecruitServiceLocator.getRecruitUserService().updateCandidateInfo(candidate);
				}
				catch (DataException de) {
					log.error("execute() - DataException setting resume with business delegate", de);
					throw new SystemException("execute() - DataException setting resume with business delegate", de);
				}
		        log.debug("doPerform() - finished.");
    		}
        }
        return("success");
    }
}
